<?php

namespace app\admin\model;

use app\common\model\TimeModel;

/**
 * @property int $id ID 
 * @property \app\admin\model\User $user 用户信息 
 * @property int $charge_rule_id 充值规则id 
 * @property string $order_sn 订单编号 
 * @property float|double $total_money 金额 
 * @property float|double $charge_money 充值金额 
 * @property int $pay_status 支付状态 1:已支付,0:未支付
 * @property int $pay_time 支付时间 
 * @property int $create_time 创建时间 
 */
class OrderRecharge extends TimeModel
{

    protected $name = "order_recharge";

    protected $deleteTime = false;

    
    public const SELECT_LIST_PAY_STATUS = ['1'=>'已支付','0'=>'未支付',];

    
    
    public function user()
    {
        return $this->belongsTo('\app\admin\model\User', 'user_id', 'id');
    }

    public static function createRechargeOrder($data,$user)
    {
        $charge_rule = \app\admin\model\UserRechargeRule::where('id',$data['charge_rule_id'])->find();
        
        if(!$charge_rule){
            exception('充值规则不存在');
        }
        $money = $charge_rule['money'];
        $orderData['user_id'] = $user['id'];
        $orderData['order_sn'] = get_order_sn('R');
        $orderData['total_money'] = $money;
        $orderData['charge_money'] = $money;
        $orderData['charge_rule_id'] = $data['charge_rule_id'];
        //插入订单充值表
        $order = self::create($orderData);
        
        //插入订单主表
        \app\admin\model\Order::create([
            'user_id' => $user['id'],
            'order_type' => 1,
            'order_sn' => $orderData['order_sn'],
            'order_money' => $money,
            'payable_money' => $money,
        ]);
        return [
            'order_sn' => $orderData['order_sn'],
            'money' => $money
        ];
    }
}